home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3669 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: ibm32.perftech.com!usenet
  2. From: murf@perftech.com (John Murphy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Sorting large files
  5. Date: 30 Jan 1996 16:38:44 GMT
  6. Organization: Performance Technology Inc
  7. Message-ID: <4elhik$cpq@ibm32.perftech.com>
  8. References: <4e8j9b$cuf@longwood.cs.ucf.edu>
  9. NNTP-Posting-Host: k5zba.perftech.com
  10. Mime-Version: 1.0
  11. NNTP-Posting-User: REVCO
  12. X-Newsreader: WinVN 0.93.11
  13.  
  14. In article <4e8j9b$cuf@longwood.cs.ucf.edu>, schnitzi@longwood.cs.ucf.edu 
  15. says...
  16. >
  17. >There was some discussion here a little while
  18. >back on how to sort the lines in a large file
  19. >without having to have a huge character array.
  20. >I suggested using ftell and fseek to hunt down
  21. >the particular lines you are comparing.  Only
  22. >just the other day did I notice (via a web
  23. >search engine) that someone posted a question
  24. >on how this could be done...  So here goes.
  25. >
  26. >
  27. >This program sorts stdin to stdout, so it would
  28. >have to be run like this:
  29. >
  30. >        sort < infile > outfile
  31. >
  32. >It should be easy enough to modify to use file
  33. >pointers, though.  It also requires a 'long'
  34. >for every line in the file, but this beats the
  35. >heck out of a huge character array.  I've 
  36. >written it here to handle up to 5000-line files.
  37. >
  38. >
  39. >#include <stdio.h>
  40. >main()
  41. >{
  42. >        long index[5000], z;
  43. >        char line1[ 256 ], line2[ 256 ];
  44. >        int i, j, count=0;
  45. >        do
  46. >        {
  47. >                index[ count++ ] = ftell( stdin );
  48. >        }
  49. >        while ( gets(line1) );
  50. >        for ( i=0; i<count-1; i++ )
  51. >        {
  52. >                for ( j=i+1; j<count; j++ )
  53. >                {
  54. >                        fseek( stdin, index[i], 0 );
  55. >                        gets( line1 );
  56. >                        fseek( stdin, index[j], 0 );
  57. >                        gets( line2 );
  58. >                        if ( strcmp( line1, line2 ) > 0 )
  59. >                        {
  60. >                                z = index[i];
  61. >                                index[i] = index[j];
  62. >                                index[j] = z;
  63. >                        }
  64. >                }
  65. >        }
  66. >        for ( i=0; i<count; i++ )
  67. >        {
  68. >                fseek( stdin, index[i], 0 );
  69. >                gets( line1 );
  70. >                puts( line1 );
  71. >        }
  72. >}
  73. >
  74. Who says computer people don't have a sense of humor?!
  75.  
  76. Least any neophytes miss the joke and take this as a serious solution to the
  77. sorting problem, perhaps we should point out that while this code will
  78. compile and run and even (eventually) sort a file, it's practical use will
  79. be limited by the MTBF of your equipment and/or your life expectancy ---
  80. it's SLOOOW!  Any file I/O operation is time consuming, and depending on the
  81. file system, random file I/O can be VERY time consuming; with MS-DOS, for
  82. instance, reading a large file backwards is an N-squared operation.
  83.  
  84. As a sanity check on the practicality of this method of sorting, try sorting
  85. a 20,000 byte file; that's a file that would require the same amount of
  86. memory as the array of 5000 longs.  On one of my systems, using this code to
  87. sort a file of 250 80 bytes records took 17:30 minutes; sorting the same
  88. file with MS-DOS SORT (a very slow sort program) took 0.8 seconds.
  89.  
  90. The trick to sorting large files in a reasonable amount of time is to
  91. minimize the file I/O.  As usual, see Knuth for details...
  92.  
  93. Murf
  94.  
  95.